package view;


import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.tree.DefaultMutableTreeNode;

import controller.FileController;
import model.MyJTree;
import model.NodeElement;
import model.Program;
import model.Programs;

public class FileView extends JPanel {
	
	private FileController fileController;
	private String nomClient;
	
	private JTabbedPane tabbedPane;
	private MyJTree treeUsers;
	private MyJTree treeShared;
	
	public FileView(String nomClient, FileController fileController){
		
		this.nomClient = nomClient;
		this.fileController = fileController;
		this.tabbedPane = new JTabbedPane();
		
		this.setBorder(BorderFactory.createTitledBorder("File"));
		this.setPreferredSize(new Dimension(240, 500));		
		
		this.setLayout(new BorderLayout());
		
		this.treeUsers = new MyJTree("Users", this.fileController);
		tabbedPane.addTab("Users", this.treeUsers);
		
		this.treeShared = new MyJTree("Shared", this.fileController);
		tabbedPane.addTab("Shared", this.treeShared);
		
		this.add(tabbedPane, BorderLayout.CENTER);
	}


	public DefaultMutableTreeNode creerNodeApplicationTree(String nomApp){
		DefaultMutableTreeNode application;
		application= new DefaultMutableTreeNode(new NodeElement(nomApp, NodeElement.APPLICATION));
		return application;
	}

	public DefaultMutableTreeNode creerNodeApplicationTreeAvecProg( Program prog){
		DefaultMutableTreeNode application;
		application= new DefaultMutableTreeNode(new NodeElement(prog.getProgramName(), NodeElement.APPLICATION, prog));
		return application;
	}
	
	public DefaultMutableTreeNode creerNodeDossierTree(String nomDossier){
		DefaultMutableTreeNode dossier;
		dossier= new DefaultMutableTreeNode(new NodeElement(nomDossier, NodeElement.FOLDER));
		return dossier;
	}
	
	public void addNewUserToTreePerso(String userName){		
		DefaultMutableTreeNode personne = new DefaultMutableTreeNode(new NodeElement(userName, NodeElement.PERSON));
		
		DefaultMutableTreeNode dossier = new DefaultMutableTreeNode(new NodeElement("src", NodeElement.FOLDER));
		personne.add(dossier);
		this.treeUsers.getRoot().add(personne);
		this.treeUsers.refresh();		
	}

	public void addNewUserToTreePartage(String userName){
		NodeElement nF2 = new NodeElement(userName, NodeElement.PERSON);
		DefaultMutableTreeNode personne = new DefaultMutableTreeNode(nF2);
		
		DefaultMutableTreeNode dossier;		
		dossier= new DefaultMutableTreeNode(new NodeElement("src", NodeElement.FOLDER));
		personne.add(dossier);
		this.treeShared.getRoot().add(personne);
		this.treeShared.refresh();
	}
	
	public void refreshTreeUsers(){
		this.treeUsers.refresh();
	}
	
	public void refreshTreeShared(){
		this.treeShared.refresh();
	}
	
	public void initialProgrammes(Programs lesProgs){
		for(int i=0; i<lesProgs.getSize(); i++){
			initialProgramme(lesProgs.getProgramme(i));
		}
	}
	
	public void initialProgramme(Program prog){
		DefaultMutableTreeNode person;
		DefaultMutableTreeNode folder;
		DefaultMutableTreeNode application;
		int indiceUserRecher=0, indiceDossierRecher=0, indiceApplicationRecher=0;
		String userRecherche, dossierRecherche, nomProg;
		userRecherche = prog.getOwner();
		dossierRecherche =  prog.getFolderName();
		nomProg = prog.getProgramName();
		
		DefaultMutableTreeNode root = this.treeUsers.getRoot();
		application = creerNodeApplicationTreeAvecProg(prog);
		
		if(root.getChildCount() > 0){
			indiceUserRecher = nodeRechercherTree(root, userRecherche, NodeElement.PERSON);
			if(indiceUserRecher >= 0 ){
				
				DefaultMutableTreeNode nodeUserRecher = (DefaultMutableTreeNode) root.getChildAt(indiceUserRecher);
				indiceDossierRecher = nodeRechercherTree(nodeUserRecher, dossierRecherche, NodeElement.FOLDER );
			
				if(indiceDossierRecher >=0){					
					DefaultMutableTreeNode nodeDossierRecher = (DefaultMutableTreeNode) nodeUserRecher.getChildAt(indiceDossierRecher);
					indiceApplicationRecher = nodeRechercherTree(nodeDossierRecher, nomProg, NodeElement.APPLICATION );
					
					if(indiceApplicationRecher < 0){
						folder = (DefaultMutableTreeNode) this.treeUsers.getRoot().getChildAt(indiceUserRecher).getChildAt(indiceDossierRecher);
						folder.add(application);
						refreshTreeUsers();
					}
				}else{
					folder = creerNodeDossierTree(dossierRecherche);
					folder.add(application);
					person = (DefaultMutableTreeNode) this.treeUsers.getRoot().getChildAt(indiceUserRecher);
					person.add(folder);
					refreshTreeUsers();
				}
			}else{
				System.err.println("Pb: Client doesn't exist");
			}
		}else
		{
			System.out.println("JTree est vide ");
		}
		
	}

	public int nodeRechercherTree(DefaultMutableTreeNode root, String nameNodeRecherche, String type){
		NodeElement nodeCour ;
		for(int i=0; i<root.getChildCount();i++){
			nodeCour = getNodeInfo(root, i);
			if(nodeCour.getName().equals(nameNodeRecherche) && nodeCour.getType().equals(type)){				
				return i;
			}
		}
		return -1;
	}
	
	public NodeElement getNodeInfo(DefaultMutableTreeNode node, int index){		
		return (NodeElement) ((DefaultMutableTreeNode) node.getChildAt(index)).getUserObject();
	}
	
	public void traiterProgrammePartage(Program progPartage){
		DefaultMutableTreeNode personne;
		DefaultMutableTreeNode folder;
		DefaultMutableTreeNode application;
		int indiceUserRecher=0, indiceDossierRecher=0, indiceApplicationRecher=0;
		String userRecherche, dossierRecherche, nomProg;
		userRecherche = progPartage.getOwner();
		dossierRecherche =  progPartage.getFolderName();
		nomProg = progPartage.getProgramName();
		
		DefaultMutableTreeNode root = this.treeShared.getRoot();
		application = creerNodeApplicationTreeAvecProg(progPartage);
		
		if(root.getChildCount()>0){
			if(root!= null){				
				indiceUserRecher = nodeRechercherTree(root, userRecherche, NodeElement.PERSON);
				if(indiceUserRecher >= 0 ){
					
					DefaultMutableTreeNode nodeUserRecher = (DefaultMutableTreeNode) root.getChildAt(indiceUserRecher);
					indiceDossierRecher = nodeRechercherTree(nodeUserRecher, dossierRecherche, NodeElement.FOLDER );
					
					if(indiceDossierRecher >=0){					
						DefaultMutableTreeNode nodeDossierRecher = (DefaultMutableTreeNode) nodeUserRecher.getChildAt(indiceDossierRecher);						
						indiceApplicationRecher = nodeRechercherTree(nodeDossierRecher, nomProg, NodeElement.APPLICATION );
						
						if(indiceApplicationRecher < 0){
							folder = (DefaultMutableTreeNode) this.treeShared.getRoot().getChildAt(indiceUserRecher).getChildAt(indiceDossierRecher);
							folder.add(application);
							refreshTreeUsers();
						}
					}else{
						folder = creerNodeDossierTree(dossierRecherche);
						folder.add(application);
						personne = (DefaultMutableTreeNode) this.treeShared.getRoot().getChildAt(indiceUserRecher);
						personne.add(folder);
						refreshTreeShared();
					}
				}else{
					// Name client NON existe
					System.err.println("Pb: Client doesn't exist");
				}
			}
		}		
	}
	
	public void removeUser(String userName){
		removeUserPerso(userName);
		removeUserPartage(userName);
	}
	
	public void removeUserPerso(String userName){
		int indiceUserRecher = -1 ;
		DefaultMutableTreeNode root = this.treeUsers.getRoot();
		
		if(root.getChildCount()>0){
			if(root!= null){
				indiceUserRecher = nodeRechercherTree(root, userName, NodeElement.PERSON);
				if(indiceUserRecher >= 0 ){
					root.remove(indiceUserRecher);
					refreshTreeUsers();
				}
			}
		}
	}
	
	public void removeUserPartage(String userName){
		int indiceUserRecher = -1 ;
		DefaultMutableTreeNode root = this.treeShared.getRoot();
		
		if(root.getChildCount()>0){
			if(root!= null){
				indiceUserRecher = nodeRechercherTree(root, userName, NodeElement.PERSON);
				if(indiceUserRecher >= 0 ){
					root.remove(indiceUserRecher);
					refreshTreeShared();
				}
			}
		}
	}
	
	
	
	// ----------- Getters/Setters -----------
	
	public String getNomClient() {
		return nomClient;
	}


	public void setNomClient(String nomClient) {
		this.nomClient = nomClient;
	}

}
	
	